package com.leyou.search.web;

import com.leyou.search.entity.Goods;
import com.leyou.search.entity.SearchParamDTO;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.elasticsearch.action.search.SearchRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * 初始化索引库
     */
    @GetMapping("initialization")
    public ResponseEntity<String> init() {
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }

    @GetMapping("/suggestion")
    public Mono<List<String>>getSuggest(
            @RequestParam("key")String key){
        return this.searchService.getSuggest(key);
    }

    /**
     * 分页搜索商品数据
     * @param request
     * @return
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(
            @RequestBody SearchParamDTO request
            ){
        return this.searchService.searchGoods(request);

    }

    @PostMapping("/filter")
    public Mono<Map<String,List<?>>> listFilter(
            @RequestBody SearchParamDTO searchParamDTO
    ){
        return this.searchService.listFilter(searchParamDTO);
    }
}
